<?php
class Login extends KF_Controller
{
    function index()
    {
        if (IS_POST)
        {
            $mno = isset($_POST['mno']) ? Util::make_str_replace($_POST['mno']) : '';
            $pwd = isset($_POST['pwd']) ? Util::make_str_replace($_POST['pwd']) : '';

            if (empty($mno)) $this->error('手机号不能为空');
            if (empty($pwd)) $this->error('密码不能为空');

            $pwd = md5($pwd);
            $sql = "select id,mobile,nickname,status from kfsy_sjxt.kf_auth_users where mobile='$mno' and password='$pwd'";
            $user = DB::select_db(1, $sql, 0);
            if (empty($user))
            {
                $this->error('用户名或密码无效');
            }
            else if ($user['status'] == 1)
            {
                $uid = intval($user['id']);
                $ip = Util::fetch_real_ip();
                $sql = "update kfsy_sjxt.kf_auth_users set login_time=now(),login_ip='$ip' where id='$uid'";
                DB::update_db($uid, $sql);

                // $_SESSION['user'] = array('uid' => $uid, 'mobile' => $user['mobile'], 'nickname' => $user['nickname']);
                $userInfo = array(
                        'uid' => $uid, 
                        'mobile' => $user['mobile'], 
                        'nickname' => $user['nickname'],
                        'expire' => time() + 86400
                );

                $userInfo = json_encode($userInfo);
                $userInfoStr = $this->encode($userInfo);
                setcookie('login_user_info',$userInfoStr,time() + 86400);

                $this->success('登录成功', 'home/index');
            }
            else if ($user['status'] == 3)
            {
                $this->error('用户信息已失效');
            }
            else
            {
                $this->error('用户信息还未审核');
            }
        }

        if(isset($_GET['old'])){
            $this->load->view('home/login');
        }else{
            $this->load->view('home/new_login');
        }
        
    }    

    function logout()
    {
        session_unset();
        session_destroy();
        $this->redirect('/login');
    }

    function code()
    {
        $this->load->library('vcode');
        session_start();
        if ($_GET['a'] == 'e')
        {
            /*
            *验证码显示
            */
            //验证码的 宽，高，汉字数量
            $vcode = new Vcode('200', '200', '4');
            //将验证码图片输出
            $vcode->outimg();
        }
        elseif ($_GET['a'] == 'v')
        {
            /*
            *点击汉字判断
            */
            if ($_GET['k'])
            {
                $arr = explode(',', $_GET['k']);
            }
            else
            {
                echo json_encode('no');
                exit;
            }
            $d = $_SESSION['vcode'];
            $min_x = $d['x'];
            $max_x = $d['x'] + $d['size'] * 3 + 10;

            $min_y = $d['y'] - $d['size'] - 3;
            $max_y = $d['y'] + 3;

            if (($arr[0] >= $min_x && $arr[0] <= $max_x) && ($arr[1] >= $min_y && $arr[1] <= $max_y))
            {
                echo json_encode('ok');
            }
            else
            {
                echo json_encode('no');
            }
            exit;
        }
        elseif ($_GET['a'] == 'g')
        {
            /*
            *获取所需要点击的字
            */
            echo json_encode($_SESSION['vcode']['code']);
            exit;
        }
        else
        {
            exit('错误指令');
        }
    }

    function rw()
    {
        $val = isset($_GET['v']) ? trim($_GET['v']) : '';
        if (empty($val)) $val = 'zzz';
        echo '<pre>';
        var_dump($this->db->select('*')->where('id', 1)->get('kfsy_sjxt.kf_auth_users')->result());
        var_dump($this->db->where('id', 1)->set('nickname', $val)->update('kfsy_sjxt.kf_auth_users'));
        var_dump($this->db->select('*')->where('id', 1)->get('kfsy_sjxt.kf_auth_users')->result());
    }

    function cc()
    {
        $this->load->driver('cache');

        echo '<pre>';

        var_dump($this->cache->memcached->save('foo', 'bar1111', 10));
        var_dump($this->cache->memcached->get('foo'));

        echo '--------------------------<br>';

        var_dump($this->cache->redis->save('foo', 'bar', 10));
        var_dump($this->cache->redis->get('foo'));
    }
}
